package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.WdclwcxkyqdBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdclwcxkyqdBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.WdclwcxkyqdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclwcxkyqdbgBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclwcxkyqdbgBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdclwcxkyqdbgBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdclwcxkyqdbgBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdclwcxkyqdbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料无侧限抗压强度试验检测报告Service业务层处理
 *
 * @author zyb
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclwcxkyqdbgBsInfoServiceImpl implements IWdclwcxkyqdbgBsInfoService {

    private final WdclwcxkyqdbgBsInfoMapper baseMapper;
    private final WdclwcxkyqdBaDetailMapper detailMapper;

    /**
     * 查询无机结合料稳定材料无侧限抗压强度试验检测报告
     */
    @Override
    @FIllBasicInfo
    public WdclwcxkyqdbgBsInfoVo queryById(Long excelId) {
        WdclwcxkyqdbgBsInfoVo wdclwcxkyqdbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdclwcxkyqdBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdclwcxkyqdBaDetail>()
            .eq(WdclwcxkyqdBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclwcxkyqdbgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(wdclwcxkyqdbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(wdclwcxkyqdbgBsInfoVo.getJiancjl())) {
            return wdclwcxkyqdbgBsInfoVo;
        }
        return jcjl(wdclwcxkyqdbgBsInfoVo);
    }

    /**
     * 查询无机结合料稳定材料无侧限抗压强度试验检测报告列表
     */
    @Override
    public TableDataInfo<WdclwcxkyqdbgBsInfoVo> queryPageList(WdclwcxkyqdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclwcxkyqdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdclwcxkyqdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料无侧限抗压强度试验检测报告列表
     */
    @Override
    public List<WdclwcxkyqdbgBsInfoVo> queryList(WdclwcxkyqdbgBsInfoBo bo) {
        LambdaQueryWrapper<WdclwcxkyqdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclwcxkyqdbgBsInfo> buildQueryWrapper(WdclwcxkyqdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclwcxkyqdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdclwcxkyqdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdclwcxkyqdbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), WdclwcxkyqdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdclwcxkyqdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdclwcxkyqdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdclwcxkyqdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdclwcxkyqdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdclwcxkyqdbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdclwcxkyqdbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdclwcxkyqdbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), WdclwcxkyqdbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdclwcxkyqdbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdclwcxkyqdbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdclwcxkyqdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdclwcxkyqdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdclwcxkyqdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), WdclwcxkyqdbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), WdclwcxkyqdbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), WdclwcxkyqdbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), WdclwcxkyqdbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdclwcxkyqdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), WdclwcxkyqdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), WdclwcxkyqdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdclwcxkyqdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdclwcxkyqdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), WdclwcxkyqdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdclwcxkyqdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdclwcxkyqdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), WdclwcxkyqdbgBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getShijzbff()), WdclwcxkyqdbgBsInfo::getShijzbff, bo.getShijzbff());
        lqw.eq(bo.getWendjjl() != null, WdclwcxkyqdbgBsInfo::getWendjjl, bo.getWendjjl());
        lqw.eq(bo.getZuidgmd() != null, WdclwcxkyqdbgBsInfo::getZuidgmd, bo.getZuidgmd());
        lqw.eq(bo.getZuijhsl() != null, WdclwcxkyqdbgBsInfo::getZuijhsl, bo.getZuijhsl());
        lqw.eq(bo.getChengxysd() != null, WdclwcxkyqdbgBsInfo::getChengxysd, bo.getChengxysd());
        lqw.eq(bo.getZhijrq() != null, WdclwcxkyqdbgBsInfo::getZhijrq, bo.getZhijrq());
        lqw.eq(bo.getShiyrq() != null, WdclwcxkyqdbgBsInfo::getShiyrq, bo.getShiyrq());
        lqw.eq(bo.getLingq() != null, WdclwcxkyqdbgBsInfo::getLingq, bo.getLingq());
        lqw.eq(bo.getJiazsl() != null, WdclwcxkyqdbgBsInfo::getJiazsl, bo.getJiazsl());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglcpbl()), WdclwcxkyqdbgBsInfo::getKuanglcpbl, bo.getKuanglcpbl());
        lqw.eq(bo.getShejqd() != null, WdclwcxkyqdbgBsInfo::getShejqd, bo.getShejqd());
        lqw.eq(bo.getBaozl() != null, WdclwcxkyqdbgBsInfo::getBaozl, bo.getBaozl());
        lqw.eq(bo.getBaozlxsza() != null, WdclwcxkyqdbgBsInfo::getBaozlxsza, bo.getBaozlxsza());
        lqw.eq(bo.getPingjkyqd() != null, WdclwcxkyqdbgBsInfo::getPingjkyqd, bo.getPingjkyqd());
        lqw.eq(bo.getZuidz() != null, WdclwcxkyqdbgBsInfo::getZuidz, bo.getZuidz());
        lqw.eq(bo.getZuixz() != null, WdclwcxkyqdbgBsInfo::getZuixz, bo.getZuixz());
        lqw.eq(bo.getDaibz() != null, WdclwcxkyqdbgBsInfo::getDaibz, bo.getDaibz());
        lqw.eq(bo.getBiaozcs() != null, WdclwcxkyqdbgBsInfo::getBiaozcs, bo.getBiaozcs());
        lqw.eq(bo.getPiancxscv() != null, WdclwcxkyqdbgBsInfo::getPiancxscv, bo.getPiancxscv());
        lqw.eq(bo.getQualId() != null, WdclwcxkyqdbgBsInfo::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), WdclwcxkyqdbgBsInfo::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, WdclwcxkyqdbgBsInfo::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, WdclwcxkyqdbgBsInfo::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), WdclwcxkyqdbgBsInfo::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, WdclwcxkyqdbgBsInfo::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), WdclwcxkyqdbgBsInfo::getJiegpd, bo.getJiegpd());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料无侧限抗压强度试验检测报告
     */
    @Override
    public Boolean insertByBo(WdclwcxkyqdbgBsInfoBo bo) {
        WdclwcxkyqdbgBsInfo add = BeanUtil.toBean(bo, WdclwcxkyqdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 13次
            ArrayList<WdclwcxkyqdBaDetail> details = new ArrayList<>(13);
            for (long i = 1; i <= 13L; i++) {
                details.add(new WdclwcxkyqdBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料无侧限抗压强度试验检测报告
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(WdclwcxkyqdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        WdclwcxkyqdbgBsInfo update = BeanUtil.toBean(bo, WdclwcxkyqdbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdclwcxkyqdBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(WdclwcxkyqdbgBsInfoBo bo) {
        if (BigDecimalUtil.defaultValue(bo.getDaibz()).compareTo(BigDecimalUtil.defaultValue(bo.getShejqd())) > 0) {
            bo.setExcelResult(2);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclwcxkyqdbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料无侧限抗压强度试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public WdclwcxkyqdbgBsInfoVo jcjl(WdclwcxkyqdbgBsInfoVo vo) {
        //        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，所检指标符合《水泥稳定碎石基层施工技术规范》DB37/T 3577-2019及设计文件要求。");
        return vo;
    }
}
